import 'package:flutter/material.dart';
import 'package:fluwx/fluwx.dart' as fluwx;


import 'package:flutter/cupertino.dart';
import 'package:aweb5/config/Config.dart';
import 'package:convert/convert.dart';
import 'package:crypto/crypto.dart';
import 'dart:convert';
import 'package:fluttertoast/fluttertoast.dart';
import 'dart:io';
import 'package:flutter/services.dart';

class fluwxUtil   {
   static void init(String appid,String link){
     fluwx.registerWxApi(appId:appid,universalLink:link);
   }

   static const platform = const MethodChannel('system_share_wx_muti_img');
//   static const platform1 = const MethodChannel('sharevideomethod');

   static Future<Null> share_mutiimg_wechat_system(Map map) async {

//     List img = List();
//     img.add("this is first");
//     img.add("this is seconde");
//
//     Map map = Map();
//     map['imgs'] = img;
     try {
       final int result = await platform.invokeMethod('sharemutiimg',map);

     } on PlatformException catch (e) {

     }


   }


  static Future<Null> share_mutiimg_wechat_timeline(Map map) async {

//     List img = List();
//     img.add("this is first");
//     img.add("this is seconde");
//
//     Map map = Map();
//     map['imgs'] = img;
    try {
      final int result = await platform.invokeMethod('sharetotimeline',map);

    } on PlatformException catch (e) {

    }


  }


   static shareImg(String img){

     //fluwx.shareToWeChat(fluwx.WeChatShareImageModel(fluwx.WeChatImage.file(source),thumbnail: ""));

   }


   static Future<Null> share_video(Map map) async{

     try {
       final int result = await platform.invokeMethod('sharethefuckvideo',map);
//       final int result = await platform.invokeMethod('sharemutiimg',map);

     } on PlatformException catch (e) {

     }

   }

   static void shareLinkToSession(String url,String title,String thumb,String desc){

     var model = fluwx.WeChatShareWebPageModel(
       url,
       title: title,
       description: desc,
       thumbnail: fluwx.WeChatImage.network(thumb),
       scene: fluwx.WeChatScene.SESSION,
     );
     fluwx.shareToWeChat(model);

   }

   static void shareOnlineImgToSession(String url){

     fluwx.WeChatImage weChatImage = fluwx.WeChatImage.network(url);
     fluwx.shareToWeChat(fluwx.WeChatShareImageModel(weChatImage  ));

   }

   static void shareOnlineVideoToSession(String url){
     var model = new fluwx.WeChatShareVideoModel(
         videoUrl: url,
//         videoLowBandUrl: _videoLowBandUrl,
         thumbnail: fluwx.WeChatImage.network("http://ettda.com/images/index/content4_leftPic.png"),
//         description: _description,
         scene: fluwx.WeChatScene.SESSION,
//         title: _title
     );
     fluwx.shareToWeChat(model);
   }


   //file://
   static void shareLocalImgToSession(String url){



//     url = url.substring(7,url.length);
     Fluttertoast.showToast(msg: url);
     File f;
     try {
         f = File(url.substring(7,url.length));
     }catch(e){
       Fluttertoast.showToast(msg: e.toString());
     }
   f.exists().then((value) => (){
     if(value){
       Fluttertoast.showToast(msg: "exist");
     }else{
       Fluttertoast.showToast(msg: "不存在");
     }


   });
    String url1= Uri.encodeComponent(url);
    Fluttertoast.showToast(msg: url1);
//     substring(7,url.length)
     fluwx.shareToWeChat(

        // fluwx.WeChatShareImageModel(fluwx.WeChatImage.file(new File(url1.substring(7,url1.length))), thumbnail: fluwx.WeChatImage.file(new File(url1.substring(7,url1.length)))));
         fluwx.WeChatShareImageModel(fluwx.WeChatImage.file(new File("/storage/emulated/0/AplusDownload/1594952435353x0x.jpg"))));
   }
//file://
   static void shareLocalVideoToSession(String url){
     var model = new fluwx.WeChatShareVideoModel(
         videoUrl: url,
//         videoLowBandUrl: _videoLowBandUrl,
//         thumbnail: WeChatImage.network(_thumnail),
//         description: _description,
         scene: fluwx.WeChatScene.SESSION,
//         title: _title
     );
     fluwx.shareToWeChat(model);
   }

   static void shareTextToSession(String text){

     fluwx.shareToWeChat(fluwx.WeChatShareTextModel(text, scene: fluwx.WeChatScene.SESSION)).then((data) {
       print("-->$data");
     });

   }



   static void shareMiniProToSession(String webPageUrl,String userName,String title,String path,String description, String thumbnail ){

     var model = new fluwx.WeChatShareMiniProgramModel(
         webPageUrl:  webPageUrl,
         userName:  userName,
         title:  title,
         path:  path,
         description:  description,
         thumbnail: fluwx.WeChatImage.network( thumbnail)
     );
     fluwx.shareToWeChat(model);

   }


//   static void shareImagetoSession(String imgUrl,String transaction){
//     fluwx.share(fluwx.WeChatShareImageModel(
//         image:    imgUrl,
//
//         transaction: transaction,
//         scene: fluwx.WeChatScene.SESSION,
//         description: "image"));
//   }
//   static void shareImagetoFavorite(String imgUrl,String transaction){
//     fluwx.share(fluwx.WeChatShareImageModel(
//         image:    imgUrl,
//
//         transaction: transaction,
//         scene: fluwx.WeChatScene.TIMELINE,
//         description: "image"));
//   }
//   static void shareLinktoFavorite(String url,String title){
//     var model = fluwx.WeChatShareWebPageModel(
//         webPage: url,
//         title: title,
//
//         scene: fluwx.WeChatScene.TIMELINE,
//         transaction: "hh");
//     fluwx.share(model);
//   }
//   static void shareLinktoSession(String url,String title,String thumb,String desc){
//     var model = fluwx.WeChatShareWebPageModel(
//         webPage: url,
//         title: title,
//         thumbnail: thumb,
//         description: desc,
//         scene: fluwx.WeChatScene.SESSION,
//         transaction: "hh");
//     fluwx.share(model);
//   }
   static void wxpay(String appId,String partnerId,String packageValue,String nonceStr,int timeStamp, String sign,String signType,String extData,String prepatId){
     Map<String,dynamic> map =Map();
     map["appid"]=appId;
     map["partnerid"]=partnerId;
     map["prepayid"]=appId;
     map["package"]="Sign=WXPay";
     map["noncestr"]=appId;
     map["timestamp"]=appId;
     map["sign"]=appId;


     fluwx.payWithWeChat(appId: appId,partnerId: partnerId,packageValue: packageValue,nonceStr: nonceStr,timeStamp: timeStamp,sign:sign,signType: signType,extData: extData ,prepayId: prepatId).then((v){
       print(v);
     });
   }
   static String wxpay_md5(Map<String,String> map){

     String s ="";
     List<String> keys = map.keys.toList();
     // key排序
//     keys.sort((a, b) {
//       List<int> al = a.codeUnits;
//       List<int> bl = b.codeUnits;
//       for (int i = 0; i < al.length; i++) {
//         if (bl.length <= i) return 1;
//         if (al[i] > bl[i]) {
//           return 1;
//         } else if (al[i] < bl[i]) return -1;
//       }
//       return 0;
//     });
     keys.sort();
//     map.forEach((key,value){
//       print(key);
//       print(value);
//     });
     print("${keys.length}");
      for(String string in keys){
        print(string);
        map[string];
        s= s+string+"="+map[string]+"&";
      }
     print(s);
      s= s.substring(0,s.length-1);

      String stringSignTemp=s+"&key=${Config.wxkey}";
      String result = generateMd5(stringSignTemp).toUpperCase();
      return result;
   }

   static String generateMd5(String data) {
     var content = new Utf8Encoder().convert(data);
     var digest = md5.convert(content);
     // 这里其实就是 digest.toString()
     return hex.encode(digest.bytes);
   }


   static void initWX() async{
     await fluwx.registerWxApi(
         appId:"wx6e4e87b5c8f5095a",
         doOnAndroid: true,
         doOnIOS: true,
         universalLink:"https://a.91aplus.com/"

     );
//     var result = await fluwx.isWeChatInstalled();
//     print("is installed $result");
   }


   static void loginWX() {
     print("微信登录");
//     fluwx
//         .sendAuth(scope: "snsapi_userinfo", state: "wechat_sdk_demo_test")
//         .then((data) {
//
//         print("拉取微信用户信息：" + data.toString());
//         Fluttertoast.showToast(msg: data.toString());
//
//     }).catchError((e) {
//       print('weChatLogin  e  $e');
//       Fluttertoast.showToast(msg: "err$e");
//     });
   }


   static void authLogin() {

//     fluwx.responseFromAuth.listen((res){Fluttertoast.showToast(msg: "res${res.code}");});
//
//
//          fluwx.isWeChatInstalled().then((installed) {
//       if (installed) {
//         fluwx
//             .sendWeChatAuth(
//             scope: "snsapi_userinfo", state: "wechat_sdk_demo_test")
//             .then((data) {
//
//           Fluttertoast.showToast(msg: "msg${data.toString()}");
//         })
//             .catchError((e) {});
//       } else {
//         Fluttertoast.showToast(msg: "请先安装微信");
//       }
//     });
   }

}